package com.zzptc.sky.baidudemo.activities;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.zzptc.sky.baidudemo.utils.MySharedPreferences;
import com.zzptc.sky.baidudemo.R;
import com.zzptc.sky.baidudemo.base.BaseActivity;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class GuideActivity extends BaseActivity implements View.OnClickListener {

    @BindView(R.id.my_vp)
    ViewPager myVp;
    @BindView(R.id.my_ll)
    LinearLayout myLl;

    List<View> listView;

    private Button btn_ent;
    private TextView tv_tiaoguo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);

        //setContentView(R.layout.activity_guide);

        tv_tiaoguo = (TextView) findViewById(R.id.tv_tiaoguo);

        ButterKnife.bind(this);

        listView = new ArrayList<View>();

        LayoutInflater inflater = getLayoutInflater();

        View view1 = inflater.inflate(R.layout.one,null);
        View view2= inflater.inflate(R.layout.two,null);
        View view3 = inflater.inflate(R.layout.three,null);

        listView.add(view1);
        listView.add(view2);
        listView.add(view3);

        View poin1 = new View(this);
        View poin2 = new View(this);
        View poin3 = new View(this);
        poin1.setBackgroundResource(R.drawable.activity_select);
        poin2.setBackgroundResource(R.drawable.activity_select);
        poin3.setBackgroundResource(R.drawable.activity_select);

        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(10,10);
        params.leftMargin = 10;

        myLl.addView(poin1,params);
        myLl.addView(poin2,params);
        myLl.addView(poin3,params);

        myVp.setAdapter(new GuideAdapter(listView));

        myVp.setCurrentItem(0);
        poin1.setEnabled(true);
        poin2.setEnabled(false);
        poin3.setEnabled(false);


        myVp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                for (int x = 0 ; x < listView.size(); x++){
                    if (x == position){
                        myLl.getChildAt(x).setEnabled(true);
                    }else {
                        myLl.getChildAt(x).setEnabled(false);
                    }
                }
                switch (position){
                    case 0:
                        tv_tiaoguo.setVisibility(View.VISIBLE);
                        break;
                    case 1:
                        tv_tiaoguo.setVisibility(View.VISIBLE);
                        break;
                    case 2:
                        tv_tiaoguo.setVisibility(View.INVISIBLE);
                        break;
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });


        btn_ent = (Button) view3.findViewById(R.id.btn_ent);

        btn_ent.setOnClickListener(this);
        tv_tiaoguo.setOnClickListener(this);
    }

    @Override
    public int getContentLayout() {
        return R.layout.activity_guide;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_ent:
                SharedPreferences sp = MySharedPreferences.getSharedPreferences();
                SharedPreferences.Editor spe = sp.edit();
                spe.putBoolean("first",true);
                spe.commit();

                startActivity(new Intent(this,WelcomeActivity.class));

                finish();
                break;

            case R.id.tv_tiaoguo:
                myVp.setCurrentItem(listView.size()-1);
                break;
        }

    }

    class GuideAdapter extends PagerAdapter{
        private List<View> list;

        public GuideAdapter(List<View> list) {
            this.list = list;
        }

        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView(list.get(position));
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            container.addView(list.get(position));
            return list.get(position);
        }
    }
}
